﻿using ConfExpert.Forms.Base;
using ConfExpert.Properties;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Sistem
{
    public partial class Employees : XForm
    {
        public Employees()
        {
            InitializeComponent();
            txtInfo.Caption = "Personel";
        }

        private void Employees_Load(object sender, EventArgs e)
        {
            ((RepositoryItemGridLookUpEdit)gleSirket.Edit).Assign(RI(Rp.Sirket));
            gleSirket.EditValue = 1;
            gleSirket.EditValueChanged += gleSirket_EditValueChanged;
            ((RepositoryItemGridLookUpEdit)gleSirket.Edit).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            gvOverview.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gvOverview_FocusedRowChanged);
            gvOverview.DisableRowDelete();
            gvOverview.Init("GetEmployee", WhoAmI, gleSirket.EditValue.ToInt());

            gvOverview.MakeColumnInvisible("SirketId");

            if (ytk(17))
                gvOverview.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            gvOverview.ClearColumnEdit("UserId");
            gvOverview.AddAutoIncrementalPK("UserId");
            gvOverview.AddSummary();
            gvOverview.RowCellStyle += gvOverview_RowCellStyle;
            gvOverview.BestFitColumns();

            gvOverview.MakeReadOnly("DepartmentId", "TakimId", "Username", "Notes", "KartNo", "Mail1", "AXAciklama", "GercekName", "GercekSirketId");

            gvOverview.DataTable().TableNewRow += Employees_TableNewRow;

            GridManager gm = GridManager.GetManager(gvOverview);
            gm.GridMenu.AddMenu("Kullanıcıyı sil", Sil, Resources.delete_16);
            gm.GridMenu.AddMenu("Şifresini sıfırla", SifreyiSifirla);
            simpleButton1.ItemClick += simpleButton1_ItemClick;

            //gvOverview.MakeRowSelectable();

            riHyperLink.SingleClick = false;
            gvOverview.Columns["Foto"].ColumnEdit = riHyperLink;
            gvOverview.RowCellClick += gridView1_RowCellClick;
        }

        void Employees_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["GercekSirketId"] = gleSirket.EditValue.ToInt();
        }

        void gleSirket_EditValueChanged(object sender, EventArgs e)
        {
            gvOverview.RefreshDataSource(true, "GetEmployee", WhoAmI, gleSirket.EditValue.ToInt());
            gvOverview.BestFitColumns();
        }


        RepositoryItemHyperLinkEdit riHyperLink = new RepositoryItemHyperLinkEdit();

        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            Cursor.Current = Cursors.WaitCursor;

            if (e.Column.FieldName == "Foto")
            {
                DataRow dr = gvOverview.GetDataRow(e.RowHandle);
                int UserId = dr["UserId"].ToInt();
                if (UserId > 0 &&
                    dr["Foto"].ToStr() != string.Empty)
                {
                    DataTable dtDosya = ExecuteDataTable("GetUsersPhotoByUserId", UserId);

                    byte[] bytes = (byte[])dtDosya.Rows[0]["Photo1"];
                    string FileName = dtDosya.Rows[0]["Name"].ToStr() + ".jpg";

                    DevTeam.FileManager.RunTempFile(FileName, bytes);
                }
            }

            Cursor.Current = Cursors.Default;
        }

        void simpleButton1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvOverview.Update("UserId,Name,GercekSirketId,DepartmentId,TakimId,Gender,Username,Email,Mobile,Notes,KartNo,Mail1Id,AXAciklama,GercekName", "[User]");
            gvOverview.RefreshDataSource(true, "GetEmployee", WhoAmI, gleSirket.EditValue.ToInt());
        }

        void gvOverview_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "Name")
                    e.Appearance.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont, FontStyle.Bold);
        }

        void Sil(object sender, EventArgs e)
        {
            int UserId = gvOverview.GetFocusedRowCellValue("UserId").ToInt();
            if (UserId > 0)
                if (XtraMessageBox.Show("Emin misiniz?", "Teyid", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("UpdDeleteUser", UserId);
                    Notify("Kullanıcı silindi.");
                }
        }

        void SifreyiSifirla(object sender, EventArgs e)
        {
            int UserId = gvOverview.GetFocusedRowCellValue("UserId").ToInt();
            if (UserId > 0)
                if (XtraMessageBox.Show("Emin misiniz?", "Teyid", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    ExecuteNonQuery("UpdResetPassword", UserId);
                    Notify("Şifre sıfırlandı.");
                }
        }

        void gvOverview_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //DataRow dr = ((DataRowView)bs.Current).Row;
            DataRow dr = gvOverview.GetDataRow(gvOverview.FocusedRowHandle);
            txtInfo.Caption = (dr != null ? dr["Name"].ToStr() : string.Empty);
        }
    }
}